package top.suven.http.oauth.vo;

import top.suven.http.oauth.OAuthConstants;
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;

public class OAuthTokenBuilder extends BasicOAuthToken{
    private long userId;
    private String redirectUri;
    private long issuedAt;

    private OAuthTokenBuilder() {
        long time = System.currentTimeMillis();
        this.expiresIn = time + OAuthConstants.ACCESS_TOKEN_EXPIRED_MS;
    }

    public static OAuthTokenBuilder build() {

        return new OAuthTokenBuilder();
    }

    public long getUserId() {
        return userId;
    }

    public OAuthTokenBuilder setUserId(long userId) {
        this.userId = userId;
        return this;
    }

    public OAuthTokenBuilder setAccessToken(String accessToken) {
        this.accessToken = accessToken;
        return this;
    }

    public OAuthTokenBuilder setTokenType(String tokenType) {
        this.tokenType = tokenType;
        return this;
    }

    public OAuthTokenBuilder setExpiresIn(long expiresIn) {
        this.expiresIn = expiresIn;
        return this;
    }
    public String getExpires(){
       return String.valueOf(this.expiresIn);
    }

    public OAuthTokenBuilder setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
        return this;
    }

    public OAuthTokenBuilder setScope(String scope) {
        this.scope = scope;
        return this;
    }

    public OAuthTokenBuilder setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
        return this;
    }


    public OAuthTokenBuilder setIssuedAt(long issuedAt) {
        this.issuedAt = issuedAt;
        return this;
    }


    public boolean tokenExpired() {
        final long time = this.getExpiresIn() ;
        return time < System.currentTimeMillis();
    }


    public long currentTokenExpiredSeconds() {
        if (tokenExpired()) {
            return -1;
        }
        final long time = this.getExpiresIn() - System.currentTimeMillis();
        return (time > 0 ? time : -1) ;
    }
}